﻿using Newtonsoft.Json;

using PLCS.Domain.Shared.Tasks;
using PLCS.SocialHttpClient.WMS.Models;

using Volo.Abp.DependencyInjection;

namespace PLCS.SocialHttpClient.WMS
{
    public class WMSSocialService : ITransientDependency
    {
        private readonly ApiRequestService _requestService;

        public WMSSocialService(ApiRequestService requestService)
        {
            _requestService = requestService;
        }

        /// <summary>
        /// 任务报工
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="to"></param>
        /// <param name="toWarehouse"></param>
        /// <returns></returns>
        public async Task<WMSResponse> ReportTaskAsync(string taskId, int status)
        {
            WMSResponse result;
            string seqNo = Guid.NewGuid().ToString();
            try
            {
                result = await _requestService.SendWebApiRequest<WMSResponse>(PLCSSociaHttpClientConstant.wmsTaskCompletionStatusUrl, HttpMethod.Post, "WMS", "任务完成", "任务完成", new
                {
                    header = new
                    {
                        app = "wcs",
                        code = "wcs_to_wms_complete_task_feedback",
                        seqNo = seqNo,
                    },
                    body = new
                    {
                        taskId = taskId,
                        status = status
                    }
                });
            }
            catch (Exception ex)
            {
                result = new WMSResponse()
                {
                    Header = new WMSResponseHeader()
                    {
                        Code = "E",
                        Message = ex.Message,
                        SeqNo = seqNo
                    }
                };
            }

            return result;
        }

        /// <summary>
        /// 请求入库
        /// </summary>
        /// <param name="eqNo"></param>
        /// <param name="trayCode"></param>
        /// <param name="trayType"></param>
        /// <param name="warehouseType"></param>
        /// <returns></returns>
        public async Task<WMSResponse> RequestWarehousingAsync(string stationNo, string trayCode)
        {
            WMSResponse result;
            string seqNo = Guid.NewGuid().ToString();
            try
            {
                result = await _requestService.SendWebApiRequest<WMSResponse>(PLCSSociaHttpClientConstant.wmsWarehouseEntryRequestUrl, HttpMethod.Post, "WMS", "入库请求", "入库请求", new
                {
                    header = new
                    {
                        app = "wcs",
                        code = "wcs_to_wms_requset_input",
                        seqNo = seqNo,
                    },
                    body = new
                    {
                        stationNo = stationNo,
                        trayCode = trayCode,
                    }
                });
            }
            catch (Exception ex)
            {
                result = new WMSResponse()
                {
                    Header = new WMSResponseHeader()
                    {
                        Code = "E",
                        Message = ex.Message,
                        SeqNo = seqNo
                    }
                };
            }

            return result;
        }
    }
}